/*
 * 只显示门板,不显示柜体板的模式：
 * 1. 尺寸
 * 2. 四个门缝
 * 3. 铰链： 品牌、型号、规格
 * 4. 有无拉直器，型号
 * 5. 开门方向
 * 6. 厚度
 */

import * as THREE from "three";
import ContextMenu, { type MenuItem } from "@imengyu/vue3-context-menu";
import { BaseMode } from "./BaseMode";
import type { KujialeParamModel } from "../types/KujialeJsonModels/KujialeParamModel";
import {
  Part3DMesh,
  Loft3DMesh,
  MouseSenseCube,
} from "../types/DrawingGroups/meshs";
import {
  AssemblyProductGroup,
  Product3DGroup,
  Sub3DGroup,
  Base3DGroup
} from "../types/DrawingGroups/groups";
import { tag } from "@/core3d/tag";
import { TraverseAndFilter } from "../helpers/GroupTraverseHelper";

export class DoorAuditMode extends BaseMode {
  doorGroups?: Base3DGroup[];

  constructor(
    dom: HTMLCanvasElement,
    camera: THREE.Camera,
    scene: THREE.Scene
  ) {
    super(dom, camera, scene);
    this.modeName = "door audit mode";
    this.init();
  }

  init() {
    this.doorGroups = TraverseAndFilter(
      this.scene.children,
      (m) => m.prodCatId == 498,
      (m) => false,
      (cg) => {},
      (cg, cm) => {
        if(cm != undefined && cm.modelTypeId === 3){
          cg.visible = false;
        }
      }
    );
    this.doorGroups?.forEach((d) => {
      const [model] = d.model.kujialeModel!;
      const box = new THREE.Box3();
      box.setFromObject(d);
      const center = new THREE.Vector3();
      box.getCenter(center);

      const { label, div } = tag(model.modelName);
      label.position.set(center.x, center.y, center.z);
      label.center.x = 0;
      label.center.y = 0;
      this.scene.add(label);
    });
  }

  mousemove(e: MouseEvent) {
    //this.funcRaycaster(e);
  }

  mousedown(e: MouseEvent) {}

  onExitMode(): void {
    if (this.currentProductCube) {
      this.currentProductCube.visible = false;
    }

    this.scene.children.forEach((g) => {
      if (g instanceof Product3DGroup || g instanceof AssemblyProductGroup) {
        g.visible = true;
      } else if (g instanceof MouseSenseCube) {
        g.visible = false;
      }
    });
  }

  findMouseSenseCubes(): THREE.Mesh[] {
    const meshes: THREE.Mesh[] = [];
    // this.scene.children.forEach((g) => {
    //   if (
    //     g instanceof AssemblyProductGroup &&
    //     (g.model as AssemblyProductModel).kujialeAssemblyModel == this.model
    //   ) {
    //     g.children.forEach((s) => {
    //       if (s instanceof Product3DGroup || s instanceof Sub3DGroup) {
    //         if (s.children.length > 0 && s.children[0] instanceof THREE.Mesh) {
    //           meshes.push(s.children[0] as THREE.Mesh);
    //         }
    //       }
    //     });
    //   }
    // });
    return meshes;
  }
}
